package com.uuj.project.base;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;

import com.blankj.utilcode.util.ToastUtils;
import com.uuj.project.R;
import com.uuj.project.uiview.LoadDialog;
import com.uuj.project.utils.AndroidWorkaround;
import com.uuj.project.utils.ExceptionHandle;
import com.uuj.project.view.IBaseView;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * ProjectName:    ok_rxjava_retrofit
 * Package:        com.uuj.project.base
 * ClassName:      BaseActivity
 * Description:    描述
 * Author:         张继
 * CreateDate:     2019/8/8 10:34
 * UpdateUser:     更新者
 * UpdateDate:     2019/8/8 10:34
 * UpdateRemark:   更新说明
 * Version:        1.0
 */
public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity implements IBaseView {

    private Unbinder bind = null;
    protected P presenter;
    private LoadDialog load;
    private static final int FAST_CLICK_DELAY_TIME = 500;
    private static long lastClickTime;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置布局
        if (setContentView() != 0) {
            setContentView(setContentView());
        }
        if (bind == null) {
            bind = ButterKnife.bind(this);
        }
        presenter = createPresenter();
        setStatusBar();
        initToolbar(savedInstanceState);
        initData();
    }


    /**
     * 布局文件
     *
     * @return 返回布局id
     */
    protected abstract int setContentView();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 处理顶部标题栏
     *
     * @param savedInstanceState activity结束之前保存的状态
     */
    protected abstract void initToolbar(Bundle savedInstanceState);

    /**
     * 创建presenter
     */
    protected abstract P createPresenter();


    /**
     * 此处设置沉浸式地方
     */
    protected void setStatusBar() {
        // 解决兼容性问题
        if (AndroidWorkaround.checkDeviceHasNavigationBar(this)) {
            AndroidWorkaround.assistActivity(findViewById(android.R.id.content));
            getWindow().setNavigationBarColor(Color.parseColor("#FFFFFF"));
        }
        //设置状态栏背景
        AndroidWorkaround.setAndroidNativeLightStatusBar(this, true);
    }

    @Override
    public void showDialog() {
        if (load == null) {
            load = new LoadDialog(this);
        }
        load.show();
    }

    @Override
    public void showMsg(String msg, int tag) {
        if (tag == 1) {
            ToastUtils.showShort(msg);
        } else {
            ToastUtils.showLong(msg);
        }
        hiddenDialog();
    }

    @Override
    public void hiddenDialog() {
        if (load != null && load.isShowing()) {
            load.dismiss();
            load.cancel();
        }
    }

    @Override
    public void onErrorCode(ExceptionHandle.ResponseThrowable code) {
        hiddenDialog();
    }


    @Override
    public Activity getBaseActivity() {
        return this;
    }


    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        setTransitionAnimation(true);
    }


    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
        setTransitionAnimation(true);
    }

    @Override
    public void finish() {
        super.finish();
        setTransitionAnimation(false);
    }

    private void setTransitionAnimation(boolean newActivityIn) {
        if (newActivityIn) {
            // 新页面从右边进入
            overridePendingTransition(R.anim.push_right_in,
                    R.anim.push_right_out);
        } else {
            // 上一个页面从左边进入
            overridePendingTransition(R.anim.push_left_in,
                    R.anim.push_left_out);
        }
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            if (isFastClick()) {
                return true;
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    public static boolean isFastClick() {
        boolean flag = true;
        long currentClickTime = System.currentTimeMillis();
        if ((currentClickTime - lastClickTime) >= FAST_CLICK_DELAY_TIME) {
            flag = false;
        }
        lastClickTime = currentClickTime;
        return flag;
    }

    @Override
    protected void onStart() {
        presenter.onStart();
        super.onStart();
    }

    @Override
    protected void onResume() {
        presenter.onResume();
        super.onResume();
    }

    @Override
    protected void onPause() {
        presenter.onPause();
        super.onPause();
    }

    @Override
    protected void onStop() {
        presenter.onStop();
        super.onStop();
    }

    @Override
    protected void onRestart() {
        presenter.onRestart();
        super.onRestart();
    }

    @Override
    protected void onDestroy() {
        hiddenDialog();
        if (presenter != null) {
            presenter.detachView();
        }
        if (bind != null) {
            bind.unbind();
            bind = null;
        }
        super.onDestroy();
    }
}
